package com.polaris.live.dialog.luckybag

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.luckybag.LuckyBagDetailAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.DialogLuckyBagDetailBinding
import com.polaris.live.interface_bag.PAGListener
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.luckybag.LuckyBagDetailResp
import com.polaris.live.resp.go_resp.SettingModifyBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.refresh.EmptyView
import org.libpag.PAGImageView

/**
 * 福袋领取详情弹窗
 *
 * @author Created by lucas on 2024/6/11 06:40
 * @since 1.0.0
 */
class LuckyBagDetailDialog(
    context: Context,
    private val resp: LuckyBagDetailResp,
    private val viewModel: BaseViewModel,
    private val isShowAnimation: Boolean = false,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogLuckyBagDetailBinding

    private val mAdapter by lazy { LuckyBagDetailAdapter() }

    private val emptyView by lazy {
        EmptyView(context).apply {
            setEmptyTip(
                R.drawable.salalogo,
                StringUtils.getString(string.no_hava_data)
            )
        }
    }

    override fun getImplLayoutId() = R.layout.dialog_lucky_bag_detail

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogLuckyBagDetailBinding.bind(popupImplView)

        mBinding.apply {
            rvDetail.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter
            }
            tvCount.text = "${resp.receiveCount}/${resp.amount}"
            avHead.setImageUrl(resp.userBasic.avatar, resp.userBasic.userId)
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(resp.userBasic.avatarFrame))
        }
        mAdapter.setEmptyView(emptyView)
        mAdapter.addData(resp.receives)
        if (resp.userBasic.userId == UserManager.getUserId()) {
            mBinding.consDetailCenter.visibility = View.GONE
        } else {
            val list = resp.receives
            val indexOf = list.indexOfFirst { it.userBasic.userId == UserManager.getUserId() }
            if (indexOf != -1) {
                val receive = list.getOrNull(indexOf) ?: return
                initTitle(receive)

                if (isShowAnimation && receive.bombStatus.toBoolean() && resp.received) {
                    mBinding.bombPag.apply {
                        path = PagConst.LUCKY_BAG_BOMB
                        visibility = View.VISIBLE
                        setRepeatCount(1)
                        addListener(object : PAGListener {
                            override fun onAnimationEnd(p0: PAGImageView?) {
                                super.onAnimationEnd(p0)
                                mBinding.bombPag.visibility = View.GONE
                            }
                        })
                        play()
                    }
                }
            } else {
                mBinding.tvReceiveDetail.text = StringUtils.getString(string.all_lucky_bag_not_hava)
                mBinding.ivReceiveDiamond.visibility = View.GONE
                mBinding.tvReceiveNum.visibility = View.GONE
                if (!UserManager.getUser().setting?.luckyBagNotice.toBoolean()) {
                    mBinding.tvOpenNotice.text = StringUtils.getString(string.lucky_bag_open_switch)
                    mBinding.tvOpenNotice.visibility = View.VISIBLE
                    mBinding.tvOpenNotice.setOnSingleClickListener {
                        viewModel.request({
                            userService.settingModify(
                                SettingModifyBean(UserConst.UserSetting.LUCKY_BAG_NOTICE, AppConst.YES)
                            )
                        }, {
                            mBinding.tvOpenNotice.visibility = View.GONE
                            UserManager.getUser().setting?.luckyBagNotice = AppConst.YES
                            ToastGravity.showGravity(StringUtils.getString(string.lucky_bag_notice_open))
                        })
                    }
                }
            }
        }
        mBinding.ivLuckyClose.setOnSingleClickListener {
            dismiss()
        }
        mBinding.consCenterAll.setOnSingleClickListener {
            dismiss()
        }
    }

    private fun initTitle(receive: LuckyBagDetailResp.LuckyBagReceive) {
        mBinding.apply {
            tvReceiveNum.text = receive.coins.toString()
            if (receive.bombStatus.toBoolean()) {
                ivDetailBack.setImageResource(R.drawable.lucky_bag_detail_bomb_icon)
                tvReceiveDetail.text =
                    "${StringUtils.getString(string.unexpected)}\n${StringUtils.getString(string.your_receive_bomb)}"
            } else {
                tvReceiveDetail.text = StringUtils.getString(string.congratulations_receiving_it)
            }
        }
    }

    override fun getMaxWidth() = ScreenUtils.getScreenWidth()

    override fun getMaxHeight() = ScreenUtils.getScreenHeight()
}